﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using System.Data.Common;
using Watchdog.IFacade;
using Watchdog.Facade;

namespace Watchdog.Wcf {

    public partial class WatchdogDataService : IWatchdogDataService {

        #region [ Stored Procs for 'AssemblyFile' ]
        //Delete
        public void AssemblyFileDelete ( AssemblyFile assemblyfile ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("AssemblyFile_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, assemblyfile.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByDefinition
        public List<AssemblyFile> AssemblyFileGetByDefinition ( int definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("AssemblyFile_GetByDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition);
            return DataSetToAssemblyFileList (db.ExecuteDataSet (command));
        }
        //GetById
        public AssemblyFile AssemblyFileGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("AssemblyFile_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToAssemblyFile (dataset.Tables["Table"].Rows[0]);
            else
                return new AssemblyFile ();
        }

        //GetPaged
        //Insert
        public AssemblyFile AssemblyFileInsert ( AssemblyFile assemblyfile ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("AssemblyFile_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, assemblyfile.Id);
            db.AddInParameter (command, "Definition", DbType.Int32, assemblyfile.Definition);
            db.AddInParameter (command, "AssemblyName", DbType.String, assemblyfile.AssemblyName);
            db.AddInParameter (command, "Release", DbType.Binary, assemblyfile.Release);
            db.AddInParameter (command, "Debug", DbType.Binary, assemblyfile.Debug);
            db.ExecuteNonQuery (command);
            assemblyfile.Id = (int)db.GetParameterValue (command, "Id");
            return assemblyfile;
        }
        //Update
        public bool AssemblyFileUpdate ( AssemblyFile assemblyfile ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("AssemblyFile_Update");
            db.AddInParameter (command, "Id", DbType.Int32, assemblyfile.Id);
            db.AddInParameter (command, "Definition", DbType.Int32, assemblyfile.Definition);
            db.AddInParameter (command, "AssemblyName", DbType.String, assemblyfile.AssemblyName);
            db.AddInParameter (command, "Release", DbType.Binary, assemblyfile.Release);
            db.AddInParameter (command, "Debug", DbType.Binary, assemblyfile.Debug);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Config' ]
        //Delete
        public void ConfigDelete ( Config config ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Config_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, config.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByDefinition
        public List<Config> ConfigGetByDefinition ( int definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Config_GetByDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition);
            return DataSetToConfigList (db.ExecuteDataSet (command));
        }
        //GetById
        public Config ConfigGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Config_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToConfig (dataset.Tables["Table"].Rows[0]);
            else
                return new Config ();
        }

        //GetPaged
        //Insert
        public Config ConfigInsert ( Config config ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Config_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, config.Id);
            db.AddInParameter (command, "Definition", DbType.Int32, config.Definition);
            db.AddInParameter (command, "Name", DbType.String, config.Name);
            db.AddInParameter (command, "ValueType", DbType.Int32, config.ValueType);
            db.AddInParameter (command, "IsMultivalue", DbType.Boolean, config.IsMultivalue);
            db.AddInParameter (command, "IsMandatory", DbType.Boolean, config.IsMandatory);
            db.ExecuteNonQuery (command);
            config.Id = (int)db.GetParameterValue (command, "Id");
            return config;
        }
        //Update
        public bool ConfigUpdate ( Config config ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Config_Update");
            db.AddInParameter (command, "Id", DbType.Int32, config.Id);
            db.AddInParameter (command, "Definition", DbType.Int32, config.Definition);
            db.AddInParameter (command, "Name", DbType.String, config.Name);
            db.AddInParameter (command, "ValueType", DbType.Int32, config.ValueType);
            db.AddInParameter (command, "IsMultivalue", DbType.Boolean, config.IsMultivalue);
            db.AddInParameter (command, "IsMandatory", DbType.Boolean, config.IsMandatory);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'ConfigValue' ]
        //Delete
        public void ConfigValueDelete ( ConfigValue configvalue ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, configvalue.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByConfig
        public List<ConfigValue> ConfigValueGetByConfig ( int config ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_GetByConfig");
            db.AddInParameter (command, "Config", DbType.Int32, config);
            return DataSetToConfigValueList (db.ExecuteDataSet (command));
        }
        //GetById
        public ConfigValue ConfigValueGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToConfigValue (dataset.Tables["Table"].Rows[0]);
            else
                return new ConfigValue ();
        }

        //GetByTest
        public List<ConfigValue> ConfigValueGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToConfigValueList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public ConfigValue ConfigValueInsert ( ConfigValue configvalue ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, configvalue.Id);
            db.AddInParameter (command, "Test", DbType.Int32, configvalue.Test);
            db.AddInParameter (command, "Config", DbType.Int32, configvalue.Config);
            db.AddInParameter (command, "ValueObject", DbType.String, configvalue.ValueObject);
            db.ExecuteNonQuery (command);
            configvalue.Id = (int)db.GetParameterValue (command, "Id");
            return configvalue;
        }
        //Update
        public bool ConfigValueUpdate ( ConfigValue configvalue ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("ConfigValue_Update");
            db.AddInParameter (command, "Id", DbType.Int32, configvalue.Id);
            db.AddInParameter (command, "Test", DbType.Int32, configvalue.Test);
            db.AddInParameter (command, "Config", DbType.Int32, configvalue.Config);
            db.AddInParameter (command, "ValueObject", DbType.String, configvalue.ValueObject);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Definition' ]
        //AddTargetType
        public void DefinitionAddTargetType ( Definition definition, TargetType targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_AddTargetType");
            db.AddInParameter (command, "Definition", DbType.Int32, definition.Id);
            db.AddInParameter (command, "TargetType", DbType.Int32, targettype.Id);
        }
        //Delete
        public void DefinitionDelete ( Definition definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, definition.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetById
        public Definition DefinitionGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToDefinition (dataset.Tables["Table"].Rows[0]);
            else
                return new Definition ();
        }

        //GetByTargetType
        public List<Definition> DefinitionGetByTargetType ( int targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_GetByTargetType");
            db.AddInParameter (command, "TargetType", DbType.Int32, targettype);
            return DataSetToDefinitionList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public Definition DefinitionInsert ( Definition definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, definition.Id);
            db.AddInParameter (command, "Name", DbType.String, definition.Name);
            db.AddInParameter (command, "IsMultipleTarget", DbType.Boolean, definition.IsMultipleTarget);
            db.AddInParameter (command, "Description", DbType.String, definition.Description);
            db.AddInParameter (command, "Type", DbType.Int32, definition.Type);
            db.AddInParameter (command, "Created", DbType.DateTime, definition.Created);
            db.AddInParameter (command, "Modified", DbType.DateTime, definition.Modified);
            db.ExecuteNonQuery (command);
            definition.Id = (int)db.GetParameterValue (command, "Id");
            return definition;
        }
        //RemoveTargetType
        public void DefinitionRemoveTargetType ( Definition definition, TargetType targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_RemoveTargetType");
            db.AddInParameter (command, "Definition", DbType.Int32, definition.Id);
            db.AddInParameter (command, "TargetType", DbType.Int32, targettype.Id);
        }
        //Update
        public bool DefinitionUpdate ( Definition definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Definition_Update");
            db.AddInParameter (command, "Id", DbType.Int32, definition.Id);
            db.AddInParameter (command, "Name", DbType.String, definition.Name);
            db.AddInParameter (command, "IsMultipleTarget", DbType.Boolean, definition.IsMultipleTarget);
            db.AddInParameter (command, "Description", DbType.String, definition.Description);
            db.AddInParameter (command, "Type", DbType.Int32, definition.Type);
            db.AddInParameter (command, "Created", DbType.DateTime, definition.Created);
            db.AddInParameter (command, "Modified", DbType.DateTime, definition.Modified);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Notification' ]
        //AddTest
        public void NotificationAddTest ( Notification notification, Test test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_AddTest");
            db.AddInParameter (command, "Test", DbType.Int32, test.Id);
            db.AddInParameter (command, "Notification", DbType.Int32, notification.Id);
        }
        //Delete
        public void NotificationDelete ( Notification notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, notification.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetById
        public Notification NotificationGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToNotification (dataset.Tables["Table"].Rows[0]);
            else
                return new Notification ();
        }

        //GetByTest
        public List<Notification> NotificationGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToNotificationList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public Notification NotificationInsert ( Notification notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, notification.Id);
            db.AddInParameter (command, "Test", DbType.Int32, notification.Test);
            db.ExecuteNonQuery (command);
            notification.Id = (int)db.GetParameterValue (command, "Id");
            return notification;
        }
        //RemoveTest
        public void NotificationRemoveTest ( Notification notification, Test test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_RemoveTest");
            db.AddInParameter (command, "Test", DbType.Int32, test.Id);
            db.AddInParameter (command, "Notification", DbType.Int32, notification.Id);
        }
        //Update
        public bool NotificationUpdate ( Notification notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Notification_Update");
            db.AddInParameter (command, "Id", DbType.Int32, notification.Id);
            db.AddInParameter (command, "Test", DbType.Int32, notification.Test);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Schedule' ]
        public void ScheduleDelete ( Schedule schedule ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, schedule.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetById
        public Schedule ScheduleGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToSchedule (dataset.Tables["Table"].Rows[0]);
            else
                return new Schedule ();
        }
        public List<Schedule> ScheduleGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToScheduleList (db.ExecuteDataSet (command));
        }
        //GetPaged
        public Schedule ScheduleInsert ( Schedule schedule ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Test", DbType.Int32, schedule.Test);
            db.AddInParameter (command, "Paused", DbType.Boolean, schedule.Paused);
            db.AddInParameter (command, "DateStart", DbType.DateTime, schedule.DateStart);
            db.AddInParameter (command, "DateStop", DbType.DateTime, schedule.DateStop);
            db.AddInParameter (command, "HourStart", DbType.String, schedule.HourStart);
            db.AddInParameter (command, "HourStop", DbType.String, schedule.HourStop);
            db.AddInParameter (command, "RepeatType", DbType.Int16, schedule.RepeatType);
            db.AddInParameter (command, "RepeatInterval", DbType.Int16, schedule.RepeatInterval);
            db.AddInParameter (command, "Days", DbType.Int32, schedule.Days);
            db.AddInParameter (command, "Weeks", DbType.Int32, schedule.Weeks);
            db.AddInParameter (command, "Months", DbType.Int32, schedule.Months);
            db.AddInParameter (command, "NextRun", DbType.DateTime, schedule.NextRun);
            db.AddInParameter (command, "LastRun", DbType.DateTime, schedule.LastRun);
            db.AddInParameter (command, "Locked", DbType.DateTime, schedule.Locked);
            db.ExecuteNonQuery (command);
            schedule.Id = (int)db.GetParameterValue (command, "Id");
            return schedule;
        }
        public bool ScheduleUpdate ( Schedule schedule ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_Update");
            db.AddInParameter (command, "Id", DbType.Int32, schedule.Id);
            db.AddInParameter (command, "Test", DbType.Int32, schedule.Test);
            db.AddInParameter (command, "Paused", DbType.Boolean, schedule.Paused);
            db.AddInParameter (command, "DateStart", DbType.DateTime, schedule.DateStart);
            db.AddInParameter (command, "DateStop", DbType.DateTime, schedule.DateStop);
            db.AddInParameter (command, "HourStart", DbType.String, schedule.HourStart);
            db.AddInParameter (command, "HourStop", DbType.String, schedule.HourStop);
            db.AddInParameter (command, "RepeatType", DbType.Int16, schedule.RepeatType);
            db.AddInParameter (command, "RepeatInterval", DbType.Int16, schedule.RepeatInterval);
            db.AddInParameter (command, "Days", DbType.Int32, schedule.Days);
            db.AddInParameter (command, "Weeks", DbType.Int32, schedule.Weeks);
            db.AddInParameter (command, "Months", DbType.Int32, schedule.Months);
            db.AddInParameter (command, "NextRun", DbType.DateTime, schedule.NextRun);
            db.AddInParameter (command, "LastRun", DbType.DateTime, schedule.LastRun);
            db.AddInParameter (command, "Locked", DbType.DateTime, schedule.Locked);
            db.ExecuteNonQuery (command);
            return true;
        }
        public List<Schedule> ScheduleGetPending () {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_GetPending");
            return DataSetToScheduleList (db.ExecuteDataSet (command));
        }
        #endregion

        #region [ Stored Procs for 'Schedule' ]
        //    //Delete
        //public void ScheduleDelete ( Schedule schedule ) {
        //    Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
        //    DbCommand command = db.GetStoredProcCommand ( "Schedule_Delete" );
        //    db.AddInParameter ( command, "Id", DbType.Int32, schedule.Id );
        //    db.ExecuteNonQuery ( command );
        //    command = null;
        //    db = null;
        //}

        //    //Find
        //    //Get_List
        //    //GetById
        //public Schedule ScheduleGetById ( int id ) {
        //    Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
        //    DbCommand command = db.GetStoredProcCommand ( "Schedule_GetById" );
        //    db.AddInParameter ( command, "Id", DbType.Int32, id );
        //    DataSet dataset = db.ExecuteDataSet ( command );
        //    if (dataset.Tables["Table"].Rows.Count == 1)
        //        return DataRowToSchedule ( dataset.Tables["Table"].Rows[0] );
        //    else
        //        return new Schedule ();
        //}

        //    //GetByTest
        //public List<Schedule> ScheduleGetByTest ( int test  ) {
        //    Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
        //    DbCommand command = db.GetStoredProcCommand ( "Schedule_GetByTest" );
        //    db.AddInParameter ( command, "Test", DbType.Int32, test );
        //    return DataSetToScheduleList (db.ExecuteDataSet ( command ));
        //}
        //    //GetPaged
        //    //Insert
        //public Schedule ScheduleInsert ( Schedule schedule ) {
        //    Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
        //    DbCommand command = db.GetStoredProcCommand ( "Schedule_Insert" );
        //    db.AddOutParameter ( command, "Id",DbType.Int32, 0 );
        //    db.AddInParameter ( command, "Id", DbType.Int32, schedule.Id );
        //    db.AddInParameter ( command, "Test", DbType.Int32, schedule.Test );
        //    db.ExecuteNonQuery ( command );
        //    schedule.Id = (int)db.GetParameterValue ( command, "Id" );
        //    return schedule;
        //}
        //    //Update
        //public bool ScheduleUpdate ( Schedule schedule ) {
        //    Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
        //    DbCommand command = db.GetStoredProcCommand ( "Schedule_Update" );
        //    db.AddInParameter ( command, "Id", DbType.Int32 , schedule.Id );
        //    db.AddInParameter ( command, "Test", DbType.Int32 , schedule.Test );
        //    db.ExecuteNonQuery ( command );
        //    return true;
        //}

        #endregion

        #region [ Stored Procs for 'TargetDevice' ]

        public void TargetDeviceAddDevice ( int device, int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_AddDevice");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            db.AddInParameter (command, "Device", DbType.Int32, device);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        public void TargetDeviceRemoveDevice ( int device, int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_RemoveDevice");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            db.AddInParameter (command, "Device", DbType.Int32, device);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        public void TargetDeviceDelete ( TargetDevice targetdevice ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_Delete");
            db.AddInParameter (command, "Test", DbType.Int32, targetdevice.Test);
            db.AddInParameter (command, "Device", DbType.Int32, targetdevice.Device);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByTest
        public List<TargetDevice> TargetDeviceGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToTargetDeviceList (db.ExecuteDataSet (command));
        }
        //GetByTestDevice
        public List<TargetDevice> TargetDeviceGetByTestDevice ( int testdevice ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_GetByTestDevice");
            db.AddInParameter (command, "Test", DbType.Int32, testdevice);
            db.AddInParameter (command, "Device", DbType.Int32, testdevice);
            return DataSetToTargetDeviceList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public TargetDevice TargetDeviceInsert ( TargetDevice targetdevice ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_Insert");
            db.AddInParameter (command, "Test", DbType.Int32, targetdevice.Test);
            db.AddInParameter (command, "Device", DbType.Int32, targetdevice.Device);
            db.ExecuteNonQuery (command);
            return targetdevice;
        }
        //Update
        public bool TargetDeviceUpdate ( TargetDevice targetdevice ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetDevice_Update");
            db.AddInParameter (command, "Test", DbType.Int32, targetdevice.Test);
            db.AddInParameter (command, "OriginalTest", DbType.Int32, targetdevice.OriginalTest);
            db.AddInParameter (command, "Device", DbType.Int32, targetdevice.Device);
            db.AddInParameter (command, "OriginalDevice", DbType.Int32, targetdevice.OriginalDevice);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'TargetType' ]
        //AddDefinition
        public void TargetTypeAddDefinition ( TargetType targettype, Definition definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_AddDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition.Id);
            db.AddInParameter (command, "TargetType", DbType.Int32, targettype.Id);
        }
        //Delete
        public void TargetTypeDelete ( TargetType targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, targettype.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByDefinition
        public List<TargetType> TargetTypeGetByDefinition ( int definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_GetByDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition);
            return DataSetToTargetTypeList (db.ExecuteDataSet (command));
        }
        //GetById
        public TargetType TargetTypeGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToTargetType (dataset.Tables["Table"].Rows[0]);
            else
                return new TargetType ();
        }

        //GetPaged
        //Insert
        public TargetType TargetTypeInsert ( TargetType targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_Insert");
            db.AddInParameter (command, "Id", DbType.Int32, targettype.Id);
            db.AddInParameter (command, "Name", DbType.String, targettype.Name);
            db.ExecuteNonQuery (command);
            return targettype;
        }
        //RemoveDefinition
        public void TargetTypeRemoveDefinition ( TargetType targettype, Definition definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_RemoveDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition.Id);
            db.AddInParameter (command, "TargetType", DbType.Int32, targettype.Id);
        }
        //Update
        public bool TargetTypeUpdate ( TargetType targettype ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TargetType_Update");
            db.AddInParameter (command, "Id", DbType.Int32, targettype.Id);
            db.AddInParameter (command, "OriginalId", DbType.Int32, targettype.OriginalId);
            db.AddInParameter (command, "Name", DbType.String, targettype.Name);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Test' ]
        //AddNotification
        public void TestAddNotification ( Test test, Notification notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_AddNotification");
            db.AddInParameter (command, "Test", DbType.Int32, test.Id);
            db.AddInParameter (command, "Notification", DbType.Int32, notification.Id);
        }
        //Delete
        public void TestDelete ( Test test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, test.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByDefinition
        public List<Test> TestGetByDefinition ( int definition ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_GetByDefinition");
            db.AddInParameter (command, "Definition", DbType.Int32, definition);
            return DataSetToTestList (db.ExecuteDataSet (command));
        }
        //GetById
        public Test TestGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToTest (dataset.Tables["Table"].Rows[0]);
            else
                return new Test ();
        }

        //GetByNotification
        public List<Test> TestGetByNotification ( int notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_GetByNotification");
            db.AddInParameter (command, "Notification", DbType.Int32, notification);
            return DataSetToTestList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public Test TestInsert ( Test test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, test.Id);
            db.AddInParameter (command, "Name", DbType.Int32, test.Name);
            db.AddInParameter (command, "Definition", DbType.Int32, test.Definition);
            db.AddInParameter (command, "Zone", DbType.Int32, test.Zone);
            db.AddInParameter (command, "TimeOut", DbType.Int32, test.TimeOut);
            db.AddInParameter (command, "LogHistory", DbType.Boolean, test.LogHistory);
            db.AddInParameter (command, "State", DbType.Int32, test.State);
            db.AddInParameter (command, "SnoozeUntil", DbType.DateTime, test.SnoozeUntil);
            db.ExecuteNonQuery (command);
            test.Id = (int)db.GetParameterValue (command, "Id");
            return test;
        }
        //RemoveNotification
        public void TestRemoveNotification ( Test test, Notification notification ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_RemoveNotification");
            db.AddInParameter (command, "Test", DbType.Int32, test.Id);
            db.AddInParameter (command, "Notification", DbType.Int32, notification.Id);
        }
        //Update
        public bool TestUpdate ( Test test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_Update");
            db.AddInParameter (command, "Id", DbType.Int32, test.Id);
            db.AddInParameter (command, "Name", DbType.Int32, test.Name);
            db.AddInParameter (command, "Definition", DbType.Int32, test.Definition);
            db.AddInParameter (command, "Zone", DbType.Int32, test.Zone);
            db.AddInParameter (command, "TimeOut", DbType.Int32, test.TimeOut);
            db.AddInParameter (command, "LogHistory", DbType.Boolean, test.LogHistory);
            db.AddInParameter (command, "State", DbType.Int32, test.State);
            db.AddInParameter (command, "SnoozeUntil", DbType.DateTime, test.SnoozeUntil);
            db.ExecuteNonQuery (command);
            return true;
        }

        public List<Test> TestGetAll () {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Test_Get_List");
            return DataSetToTestList (db.ExecuteDataSet (command));
        }

        #endregion

        #region [ Stored Procs for 'TestHistory' ]
        //Delete
        public void TestHistoryDelete ( TestHistory testhistory ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestHistory_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, testhistory.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetById
        public TestHistory TestHistoryGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestHistory_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToTestHistory (dataset.Tables["Table"].Rows[0]);
            else
                return new TestHistory ();
        }

        //GetByTest
        public List<TestHistory> TestHistoryGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestHistory_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToTestHistoryList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public TestHistory TestHistoryInsert ( TestHistory testhistory ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestHistory_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Id", DbType.Int32, testhistory.Id);
            db.AddInParameter (command, "Test", DbType.Int32, testhistory.Test);
            db.AddInParameter (command, "Target", DbType.Int32, testhistory.Target);
            db.AddInParameter (command, "Started", DbType.DateTime, testhistory.Started);
            db.AddInParameter (command, "Stopped", DbType.DateTime, testhistory.Stopped);
            db.AddInParameter (command, "Result", DbType.Int32, testhistory.Result);
            db.AddInParameter (command, "ResultMessage", DbType.String, testhistory.ResultMessage);
            db.AddInParameter (command, "NotifySent", DbType.Boolean, testhistory.NotifySent);
            db.ExecuteNonQuery (command);
            testhistory.Id = (int)db.GetParameterValue (command, "Id");
            return testhistory;
        }
        //Update
        public bool TestHistoryUpdate ( TestHistory testhistory ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestHistory_Update");
            db.AddInParameter (command, "Id", DbType.Int32, testhistory.Id);
            db.AddInParameter (command, "Test", DbType.Int32, testhistory.Test);
            db.AddInParameter (command, "Target", DbType.Int32, testhistory.Target);
            db.AddInParameter (command, "Started", DbType.DateTime, testhistory.Started);
            db.AddInParameter (command, "Stopped", DbType.DateTime, testhistory.Stopped);
            db.AddInParameter (command, "Result", DbType.Int32, testhistory.Result);
            db.AddInParameter (command, "ResultMessage", DbType.String, testhistory.ResultMessage);
            db.AddInParameter (command, "NotifySent", DbType.Boolean, testhistory.NotifySent);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'TestStatus' ]
        //Delete
        public void TestStatusDelete ( TestStatus teststatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestStatus_Delete");
            db.AddInParameter (command, "Test", DbType.Int32, teststatus.Test);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByTest
        public List<TestStatus> TestStatusGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestStatus_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToTestStatusList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public TestStatus TestStatusInsert ( TestStatus teststatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestStatus_Insert");
            db.AddInParameter (command, "Test", DbType.Int32, teststatus.Test);
            db.AddInParameter (command, "Active", DbType.Boolean, teststatus.Active);
            db.AddInParameter (command, "Started", DbType.DateTime, teststatus.Started);
            db.AddInParameter (command, "PctComplete", DbType.Int32, teststatus.PctComplete);
            db.ExecuteNonQuery (command);
            return teststatus;
        }
        //Update
        public bool TestStatusUpdate ( TestStatus teststatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestStatus_Update");
            db.AddInParameter (command, "Test", DbType.Int32, teststatus.Test);
            db.AddInParameter (command, "OriginalTest", DbType.Int32, teststatus.OriginalTest);
            db.AddInParameter (command, "Active", DbType.Boolean, teststatus.Active);
            db.AddInParameter (command, "Started", DbType.DateTime, teststatus.Started);
            db.AddInParameter (command, "PctComplete", DbType.Int32, teststatus.PctComplete);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'TestTargetStatus' ]
        //Delete
        public void TestTargetStatusDelete ( TestTargetStatus testtargetstatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestTargetStatus_Delete");
            db.AddInParameter (command, "Test", DbType.Int32, testtargetstatus.Test);
            db.AddInParameter (command, "Target", DbType.Int32, testtargetstatus.Target);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetByTest
        public List<TestTargetStatus> TestTargetStatusGetByTest ( int test ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestTargetStatus_GetByTest");
            db.AddInParameter (command, "Test", DbType.Int32, test);
            return DataSetToTestTargetStatusList (db.ExecuteDataSet (command));
        }
        //GetByTestTarget
        public List<TestTargetStatus> TestTargetStatusGetByTestTarget ( int testtarget ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestTargetStatus_GetByTestTarget");
            db.AddInParameter (command, "Test", DbType.Int32, testtarget);
            db.AddInParameter (command, "Target", DbType.Int32, testtarget);
            return DataSetToTestTargetStatusList (db.ExecuteDataSet (command));
        }
        //GetPaged
        //Insert
        public TestTargetStatus TestTargetStatusInsert ( TestTargetStatus testtargetstatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestTargetStatus_Insert");
            db.AddInParameter (command, "Test", DbType.Int32, testtargetstatus.Test);
            db.AddInParameter (command, "Target", DbType.Int32, testtargetstatus.Target);
            db.AddInParameter (command, "Started", DbType.DateTime, testtargetstatus.Started);
            db.AddInParameter (command, "Status", DbType.String, testtargetstatus.Status);
            db.ExecuteNonQuery (command);
            return testtargetstatus;
        }
        //Update
        public bool TestTargetStatusUpdate ( TestTargetStatus testtargetstatus ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestTargetStatus_Update");
            db.AddInParameter (command, "Test", DbType.Int32, testtargetstatus.Test);
            db.AddInParameter (command, "OriginalTest", DbType.Int32, testtargetstatus.OriginalTest);
            db.AddInParameter (command, "Target", DbType.Int32, testtargetstatus.Target);
            db.AddInParameter (command, "OriginalTarget", DbType.Int32, testtargetstatus.OriginalTarget);
            db.AddInParameter (command, "Started", DbType.DateTime, testtargetstatus.Started);
            db.AddInParameter (command, "Status", DbType.String, testtargetstatus.Status);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'TestUnit' ]
        //Delete
        public void TestUnitDelete ( TestUnit testunit ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, testunit.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //Find
        //Get_List
        //GetById
        public TestUnit TestUnitGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToTestUnit (dataset.Tables["Table"].Rows[0]);
            else
                return new TestUnit ();
        }

        //GetPaged
        //Insert
        public TestUnit TestUnitInsert ( TestUnit testunit ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Name", DbType.String, testunit.Name);
            db.AddInParameter (command, "Zone", DbType.String, testunit.Zone);
            db.ExecuteNonQuery (command);
            testunit.Id = (int)db.GetParameterValue (command, "Id");
            return testunit;
        }
        //Update
        public bool TestUnitUpdate ( TestUnit testunit ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_Update");
            db.AddInParameter (command, "Id", DbType.Int32, testunit.Id);
            db.AddInParameter (command, "Name", DbType.String, testunit.Name);
            db.AddInParameter (command, "Zone", DbType.String, testunit.Zone);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion

        #region [ Stored Procs for 'Zone' ]
        //Delete
        public void ZoneDelete ( Zone zone ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Zone_Delete");
            db.AddInParameter (command, "Id", DbType.Int32, zone.Id);
            db.ExecuteNonQuery (command);
            command = null;
            db = null;
        }

        //GetById
        public Zone ZoneGetById ( int id ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Zone_GetById");
            db.AddInParameter (command, "Id", DbType.Int32, id);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToZone (dataset.Tables["Table"].Rows[0]);
            else
                return new Zone ();
        }

        //Insert
        public Zone ZoneInsert ( Zone zone ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Zone_Insert");
            db.AddOutParameter (command, "Id", DbType.Int32, 0);
            db.AddInParameter (command, "Name", DbType.String, zone.Name);
            db.ExecuteNonQuery (command);
            zone.Id = (int)db.GetParameterValue (command, "Id");
            return zone;
        }
        //Update
        public bool ZoneUpdate ( Zone zone ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Zone_Update");
            db.AddInParameter (command, "Id", DbType.Int32, zone.Id);
            db.AddInParameter (command, "Name", DbType.String, zone.Name);
            db.ExecuteNonQuery (command);
            return true;
        }

        #endregion



        #region [ Private Helpers ]
        AssemblyFile DataRowToAssemblyFile ( DataRow drow ) {
            AssemblyFile output = new AssemblyFile ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Definition"))
                output.Definition = (int)drow["Definition"];
            if (!drow.IsNull ("AssemblyName"))
                output.AssemblyName = (string)drow["AssemblyName"];
            if (!drow.IsNull ("Release"))
                output.Release = (object)drow["Release"];
            if (!drow.IsNull ("Debug"))
                output.Debug = (object)drow["Debug"];
            return output;
        }

        private List<AssemblyFile> DataSetToAssemblyFileList ( DataSet dataset ) {
            List<AssemblyFile> output = new List<AssemblyFile> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToAssemblyFile (drow));
            }
            return output;
        }

        Config DataRowToConfig ( DataRow drow ) {
            Config output = new Config ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Definition"))
                output.Definition = (int)drow["Definition"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            if (!drow.IsNull ("ValueType"))
                output.ValueType = (int)drow["ValueType"];
            if (!drow.IsNull ("IsMultivalue"))
                output.IsMultivalue = (bool)drow["IsMultivalue"];
            if (!drow.IsNull ("IsMandatory"))
                output.IsMandatory = (bool)drow["IsMandatory"];
            return output;
        }

        private List<Config> DataSetToConfigList ( DataSet dataset ) {
            List<Config> output = new List<Config> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToConfig (drow));
            }
            return output;
        }

        ConfigValue DataRowToConfigValue ( DataRow drow ) {
            ConfigValue output = new ConfigValue ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Config"))
                output.Config = (int)drow["Config"];
            if (!drow.IsNull ("ValueObject"))
                output.ValueObject = (string)drow["ValueObject"];
            return output;
        }

        private List<ConfigValue> DataSetToConfigValueList ( DataSet dataset ) {
            List<ConfigValue> output = new List<ConfigValue> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToConfigValue (drow));
            }
            return output;
        }

        Definition DataRowToDefinition ( DataRow drow ) {
            Definition output = new Definition ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            if (!drow.IsNull ("IsMultipleTarget"))
                output.IsMultipleTarget = (bool)drow["IsMultipleTarget"];
            if (!drow.IsNull ("Description"))
                output.Description = (string)drow["Description"];
            if (!drow.IsNull ("Type"))
                output.Type = (int)drow["Type"];
            if (!drow.IsNull ("Created"))
                output.Created = (DateTime)drow["Created"];
            if (!drow.IsNull ("Modified"))
                output.Modified = (DateTime)drow["Modified"];
            return output;
        }

        private List<Definition> DataSetToDefinitionList ( DataSet dataset ) {
            List<Definition> output = new List<Definition> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToDefinition (drow));
            }
            return output;
        }

        Notification DataRowToNotification ( DataRow drow ) {
            Notification output = new Notification ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            return output;
        }

        private List<Notification> DataSetToNotificationList ( DataSet dataset ) {
            List<Notification> output = new List<Notification> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToNotification (drow));
            }
            return output;
        }

        Schedule DataRowToSchedule ( DataRow drow ) {
            Schedule output = new Schedule ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Paused"))
                output.Paused = (bool)drow["Paused"];
            if (!drow.IsNull ("DateStart"))
                output.DateStart = (DateTime)drow["DateStart"];
            if (!drow.IsNull ("DateStop"))
                output.DateStop = (DateTime)drow["DateStop"];
            if (!drow.IsNull ("HourStart"))
                output.HourStart = (string)drow["HourStart"];
            if (!drow.IsNull ("HourStop"))
                output.HourStop = (string)drow["HourStop"];
            if (!drow.IsNull ("RepeatType"))
                output.RepeatType = (Int16)drow["RepeatType"];
            if (!drow.IsNull ("RepeatInterval"))
                output.RepeatInterval = (Int16)drow["RepeatInterval"];
            if (!drow.IsNull ("Days"))
                output.Days = (int)drow["Days"];
            if (!drow.IsNull ("Weeks"))
                output.Weeks = (int)drow["Weeks"];
            if (!drow.IsNull ("Months"))
                output.Months = (int)drow["Months"];
            if (!drow.IsNull ("NextRun"))
                output.NextRun = (DateTime)drow["NextRun"];
            if (!drow.IsNull ("LastRun"))
                output.LastRun = (DateTime)drow["LastRun"];
            if (!drow.IsNull ("Locked"))
                output.Locked = (DateTime)drow["Locked"];
            return output;
        }

        private List<Schedule> DataSetToScheduleList ( DataSet dataset ) {
            List<Schedule> output = new List<Schedule> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToSchedule (drow));
            }
            return output;
        }

        TargetDevice DataRowToTargetDevice ( DataRow drow ) {
            TargetDevice output = new TargetDevice ();
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Device"))
                output.Device = (int)drow["Device"];
            return output;
        }

        private List<TargetDevice> DataSetToTargetDeviceList ( DataSet dataset ) {
            List<TargetDevice> output = new List<TargetDevice> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTargetDevice (drow));
            }
            return output;
        }

        TargetType DataRowToTargetType ( DataRow drow ) {
            TargetType output = new TargetType ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            return output;
        }

        private List<TargetType> DataSetToTargetTypeList ( DataSet dataset ) {
            List<TargetType> output = new List<TargetType> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTargetType (drow));
            }
            return output;
        }

        Test DataRowToTest ( DataRow drow ) {
            Test output = new Test ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            if (!drow.IsNull ("Definition"))
                output.Definition = (int)drow["Definition"];
            if (!drow.IsNull ("Zone"))
                output.Zone = (int)drow["Zone"];
            if (!drow.IsNull ("TimeOut"))
                output.TimeOut = (int)drow["TimeOut"];
            if (!drow.IsNull ("LogHistory"))
                output.LogHistory = (bool)drow["LogHistory"];
            if (!drow.IsNull ("State"))
                output.State = (int)drow["State"];
            if (!drow.IsNull ("SnoozeUntil"))
                output.SnoozeUntil = (DateTime)drow["SnoozeUntil"];
            return output;
        }

        private List<Test> DataSetToTestList ( DataSet dataset ) {
            List<Test> output = new List<Test> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTest (drow));
            }
            return output;
        }

        TestHistory DataRowToTestHistory ( DataRow drow ) {
            TestHistory output = new TestHistory ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Target"))
                output.Target = (int)drow["Target"];
            if (!drow.IsNull ("Started"))
                output.Started = (DateTime)drow["Started"];
            if (!drow.IsNull ("Stopped"))
                output.Stopped = (DateTime)drow["Stopped"];
            if (!drow.IsNull ("Result"))
                output.Result = (int)drow["Result"];
            if (!drow.IsNull ("ResultMessage"))
                output.ResultMessage = (string)drow["ResultMessage"];
            if (!drow.IsNull ("NotifySent"))
                output.NotifySent = (bool)drow["NotifySent"];
            return output;
        }

        private List<TestHistory> DataSetToTestHistoryList ( DataSet dataset ) {
            List<TestHistory> output = new List<TestHistory> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTestHistory (drow));
            }
            return output;
        }

        TestStatus DataRowToTestStatus ( DataRow drow ) {
            TestStatus output = new TestStatus ();
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Active"))
                output.Active = (bool)drow["Active"];
            if (!drow.IsNull ("Started"))
                output.Started = (DateTime)drow["Started"];
            if (!drow.IsNull ("PctComplete"))
                output.PctComplete = (int)drow["PctComplete"];
            return output;
        }

        private List<TestStatus> DataSetToTestStatusList ( DataSet dataset ) {
            List<TestStatus> output = new List<TestStatus> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTestStatus (drow));
            }
            return output;
        }

        TestTargetStatus DataRowToTestTargetStatus ( DataRow drow ) {
            TestTargetStatus output = new TestTargetStatus ();
            if (!drow.IsNull ("Test"))
                output.Test = (int)drow["Test"];
            if (!drow.IsNull ("Target"))
                output.Target = (int)drow["Target"];
            if (!drow.IsNull ("Started"))
                output.Started = (DateTime)drow["Started"];
            if (!drow.IsNull ("Status"))
                output.Status = (string)drow["Status"];
            return output;
        }

        private List<TestTargetStatus> DataSetToTestTargetStatusList ( DataSet dataset ) {
            List<TestTargetStatus> output = new List<TestTargetStatus> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTestTargetStatus (drow));
            }
            return output;
        }

        TestUnit DataRowToTestUnit ( DataRow drow ) {
            TestUnit output = new TestUnit ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            if (!drow.IsNull ("Zone"))
                output.Zone = (int)drow["Zone"];
            return output;
        }

        private List<TestUnit> DataSetToTestUnitList ( DataSet dataset ) {
            List<TestUnit> output = new List<TestUnit> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToTestUnit (drow));
            }
            return output;
        }

        Zone DataRowToZone ( DataRow drow ) {
            Zone output = new Zone ();
            if (!drow.IsNull ("Id"))
                output.Id = (int)drow["Id"];
            if (!drow.IsNull ("Name"))
                output.Name = (string)drow["Name"];
            return output;
        }

        private List<Zone> DataSetToZoneList ( DataSet dataset ) {
            List<Zone> output = new List<Zone> (dataset.Tables["Table"].Rows.Count);
            foreach (DataRow drow in dataset.Tables["Table"].Rows) {
                output.Add (DataRowToZone (drow));
            }
            return output;
        }


        #endregion

    }
}

